package com.yaodian100.mall.shopping.web;

import java.util.Arrays;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.utils.StringUtils;
import com.yaodian100.ec.channel.service.ChannelService;
import com.yaodian100.ec.member.entity.Member;
import com.yaodian100.ec.member.service.MemberService;
import com.yaodian100.ec.order.entity.OrderPrize;
import com.yaodian100.ec.order.service.OrderPrizeService;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.service.Product2Service;
import com.yaodian100.ec.shelf.entity.ShelfTuangou;
import com.yaodian100.ec.shelf.service.ShelfTuangouService;
import com.yaodian100.mall.member.util.UserData;

public class OrderPrizeAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = -7257641866987392427L;
	private final Logger logger = LoggerFactory.getLogger(OrderPrizeAction.class);

	@Autowired
	private OrderPrizeService orderPrizeService;
	@Autowired
	private Product2Service product2Service;
	@Autowired
	private MemberService memberService;
	@Autowired
	private ShelfTuangouService shelfTuangouService;
	@Autowired
	private ChannelService channelService;

	private ShelfTuangou shelfTuangou;
	private String tuangouId;
	private String canOrder;
	private Member member;
	private String mobile;
	private String city;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
	}

	public String orderPrize() throws Exception {
		canOrder = "false";
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		shelfTuangou = shelfTuangouService.getActiveShelfTuangouByTuangouId(Long.parseLong(tuangouId));
		if (null != shelfTuangou) {
			Date today = new Date();
			if (today.after(shelfTuangou.getEndDate())) {
				canOrder = "outOfdate";// 活动已结束
				return "orderPrize";
			}
			UserData userData = new UserData(request, response);
			if (null == userData.getMemberId()) {
				canOrder = "memberNotLogin";
				return "orderPrize";
			}
			if (null != userData && StringUtils.isNotBlank(userData.getMemberId())) {
				member = memberService.get(userData.getMemberId());
			}
			Product product = product2Service.get(shelfTuangou.getProductId());
			if (!Arrays.asList(new String[] { "0", "1", "7", "2" }).contains(String.valueOf(product.getProductType()))) {
				// if (0 != product.getProductType()) {// 抽奖商品一律是一般商品
				canOrder = "errorProductType";
				return "orderPrize";
			}
			if (!"N".equals(product.getIsDelete())) {// 抽奖商品一律是N-可查询可销售
				canOrder = "errorProductIsDelete";
				return "orderPrize";
			}
			// 该抽奖活动若有设定：单个会员限购买一件时，每个会员只能抽一次
			if ("Y".equals(shelfTuangou.getMemberLimit()) && getOrderPrizeSize(tuangouId, member.getMemberId()) > 0) {
				canOrder = "errorMemberLimit";
				return "orderPrize";
			}
			if (getOrderPrizeSize(tuangouId, member.getMemberId()) == 0) {
				canOrder = "true";
				return "orderPrize";
			}
		} else {
			canOrder = "errorId";
		}
		return "orderPrize";
	}

	private int getOrderPrizeSize(String tuangouId, String memberId) throws Exception {
		CommonCriteria crit = new CommonCriteria();
		crit.addEq("tuangouId", Long.parseLong(tuangouId));
		crit.addEq("memberId", memberId);
		return orderPrizeService.getListSize(crit).intValue();
	}

	public String orderPrizeExe() throws Exception {
		HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(
				ServletActionContext.HTTP_RESPONSE);
		UserData userData = new UserData(request, response);
		shelfTuangou = shelfTuangouService.getActiveShelfTuangouByTuangouId(Long.parseLong(tuangouId));
		if (null != shelfTuangou) {
			Date today = new Date();
			if (today.after(shelfTuangou.getEndDate())) {
				canOrder = "outOfdate";// 活动已结束
				return "orderPrize";
			}
			if (null != userData && StringUtils.isNotBlank(userData.getMemberId())) {
				member = memberService.get(userData.getMemberId());
			}
			if (getOrderPrizeSize(tuangouId, member.getMemberId()) > 0) {
				canOrder = "false";// 送出后需要检查是否有重覆参加
				return "orderPrize";
			}
			int iQty = shelfTuangou.getOrderQty() + 1;
			shelfTuangou.setOrderQty(iQty);
			shelfTuangouService.saveOrUpdateShelfTuangou(shelfTuangou);
			OrderPrize orderPrize = new OrderPrize();
			orderPrize.setTuangouId(Long.parseLong(tuangouId));
			orderPrize.setMemberId(member.getMemberId());
			orderPrize.setChannel(userData.getApplySource());
			orderPrize.setChannel_child(userData.getChChildCookie());
			orderPrize.setLoginId(member.getLoginId());
			orderPrize.setOrderDate(new Date());
			orderPrize.setOrderSt("00");
			String host = request.getHeader("X-Forwarded-For");
			if (StringUtils.isBlank(host)) {
				host = StringUtils.defaultString(request.getRemoteAddr(), "0.0.0.0");
			} else if (host.length() > 100) {
				host = host.substring(0, 100);
			}
			orderPrize.setPrizeIp(StringUtils.defaultString(host, "0.0.0.0"));
			orderPrize.setPrizeMobile(mobile);
			orderPrize.setPrizePlace(city);
			orderPrize.setPrizePrice(shelfTuangou.getPrice());
			orderPrize.setProductId(shelfTuangou.getProductId());
			Product product = product2Service.get(shelfTuangou.getProductId());
			orderPrize.setProdName(product.getProductName());
			orderPrize.setPromoteName(shelfTuangou.getPromoteName());
			orderPrize.setTuangouId(shelfTuangou.getTuangouId());
			int chId = 0;
			if (StringUtils.isNotBlank(userData.readValue("ch_id"))) {
				chId = Integer.parseInt(userData.readValue("ch_id"));
			} else {
				chId = channelService.getActivateChannelIdByName(userData.getApplySource()).intValue();
			}
			orderPrize.setChannelSeq(chId);
			orderPrizeService.save(orderPrize);
			canOrder = "orderSuccess";
		} else {
			canOrder = "errorId";
		}
		return "orderPrize";
	}

	public OrderPrizeService getOrderPrizeService() {
		return orderPrizeService;
	}

	public void setOrderPrizeService(OrderPrizeService orderPrizeService) {
		this.orderPrizeService = orderPrizeService;
	}

	public Product2Service getProduct2Service() {
		return product2Service;
	}

	public void setProduct2Service(Product2Service product2Service) {
		this.product2Service = product2Service;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public ShelfTuangouService getShelfTuangouService() {
		return shelfTuangouService;
	}

	public void setShelfTuangouService(ShelfTuangouService shelfTuangouService) {
		this.shelfTuangouService = shelfTuangouService;
	}

	public ShelfTuangou getShelfTuangou() {
		return shelfTuangou;
	}

	public void setShelfTuangou(ShelfTuangou shelfTuangou) {
		this.shelfTuangou = shelfTuangou;
	}

	public String getTuangouId() {
		return tuangouId;
	}

	public void setTuangouId(String tuangouId) {
		this.tuangouId = tuangouId;
	}

	public String getCanOrder() {
		return canOrder;
	}

	public void setCanOrder(String canOrder) {
		this.canOrder = canOrder;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public ChannelService getChannelService() {
		return channelService;
	}

	public void setChannelService(ChannelService channelService) {
		this.channelService = channelService;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

}
